package com.xiangyi.teachingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyi.teachingsystem.dao.PrivateResShareMapper;
import com.xiangyi.teachingsystem.entity.dto.PrivateResUserInfoDto;
import com.xiangyi.teachingsystem.entity.po.PrivateResShare;
import com.xiangyi.teachingsystem.service.PrivateResShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PrivateResShareServiceImpl extends ServiceImpl<PrivateResShareMapper, PrivateResShare> implements PrivateResShareService {
    @Autowired
    PrivateResShareMapper privateResShareMapper;
    @Override
    public List<PrivateResShare> getShearedResList(Integer userId) {
        return privateResShareMapper.getShearedResList(userId);
    }

    @Override
    public List<PrivateResShare> getSheareResList(Integer userId) {
        return privateResShareMapper.getSheareResList(userId);
    }

    @Override
    public List<PrivateResUserInfoDto> getShareUsersListByResId(Integer resId) {
        return privateResShareMapper.getShareUsersListByResId(resId);
    }

    @Override
    @Transactional
    public void updateBatchShare(Integer resId, List<Integer> userIds) {
        privateResShareMapper.delete(new QueryWrapper<PrivateResShare>()
                .eq("res_id",resId));
        userIds.forEach((userId)->{
            PrivateResShare privateResShare = new PrivateResShare();
            privateResShare.setUserId(userId);
            privateResShare.setResId(resId);
            privateResShareMapper.insert(privateResShare);
        });
    }
}
